package com.peak.distribution.schedule;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.peak.distribution.entity.admin.DisUserLoginLog;
import com.peak.distribution.manager.admin.DisUserLoginLogManager;
import com.peak.spring.boot.builder.repository.FilterParamBuilder;
import com.peak.spring.boot.tools.DatePattern;
import com.peak.spring.boot.tools.Dates;
import com.peak.spring.boot.tools.Emptys;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class AgentSchedule {
	
	@Autowired
	private DisUserLoginLogManager loginLogManager;

	@Scheduled(cron = "${schedule.login.log}")
	public void clearLoginLog() {
		Date now = Dates.now();
		log.info("清理30天前登录日志任务启动{}", Dates.format(now, DatePattern.SHORT_LINE_TIME));
		Date last30Day = Dates.addDay(Dates.now(), -30);
		List<DisUserLoginLog> logs = loginLogManager.findList(FilterParamBuilder.create().lte("loginTime", last30Day).build());
		log.info("待清理记录{}条", Emptys.isEmpty(logs) ? 0 : logs.size());
		if (Emptys.isNotEmpty(logs)) {
			loginLogManager.delete(logs);
		}
		log.info("清理30天前登录日志结束，用时{}秒", (Dates.now().getTime() - now.getTime()) / 1000);
	}
}
